﻿Imports System.Threading

Public Class Form1
    Dim hilo1, hilo2 As Thread

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        hilo1 = New Thread(AddressOf Bucle1)
        hilo1.Start()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        hilo2 = New Thread(AddressOf Bucle2)
        hilo2.Start()
    End Sub

    Sub Bucle1()  'codeshare.io/fkrrm
        SyncLock TextBox2
            For i As Integer = 1 To Val(TextBox1.Text)
                TextBox2.Text = i
            Next
        End SyncLock
    End Sub

    Sub Bucle2()
        SyncLock TextBox4
            For i As Integer = 1 To Val(TextBox3.Text)
                TextBox4.Text = i
            Next
        End SyncLock
        
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        BackWork.RunWorkerAsync()
    End Sub

    Private Sub BackWork_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackWork.DoWork
        Dim limite As Integer = 10000
        For i As Integer = 1 To limite
            Thread.Sleep(5)
            BackWork.ReportProgress((i * 100) / limite)
        Next
    End Sub

    Private Sub BackWork_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackWork.ProgressChanged
        Try
            ProgressBar1.Value = e.ProgressPercentage
            Label1.Text = String.Format("{0}%", e.ProgressPercentage)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class
